package cn.px.rest.controller.report;

import cn.hutool.core.collection.CollectionUtil;
import cn.px.dao.vo.report.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.DateUtils;
import cn.px.common.util.R;
import cn.px.dao.dto.repair.RepairApplicationFormDTO;
import cn.px.dao.dto.repair.RepairFeeDTO;
import cn.px.dao.entity.repair.RepairFee;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.repair.ExportRepairApplicationFormVO;
import cn.px.dao.vo.export.repair.ExportRepairFeeVO;
import cn.px.dao.vo.repair.RepairApplicationFormVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.repair.RepairApplicationFormService;
import cn.px.service.repair.RepairFeeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 维修报表
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("/report/repair")
public class RepairReportController {

    private final RepairApplicationFormService repairApplicationFormService;

    private final RepairFeeService repairFeeService;

    /**
     * 1：>24小时停机次数按月统计
     * 统计规则：
     * 1：非计划性维修
     * 2：报修和维修结束的时间大于24小时,即设备停机修复时间
     * <p>
     * 2：设备故障次数按月统计
     * 统计规则：
     * 1: 非计划性维修
     * 2: 时间不限制
     *
     * @param dto  查询条件
     * @param year 查询年份
     * @return R
     */
    @GetMapping("")
    @SysLog(">24小时停机次数按月统计")
    public R getMonthReportBig24(RepairApplicationFormDTO dto, @RequestParam(required = false) Integer year, @RequestParam(required = false) LocalDate startMonth, @RequestParam(required = false) LocalDate endMonth, @RequestParam(defaultValue = "1") Integer searchType) {
        return new R(repairApplicationFormService.getMonthReportBig24(dto, year, startMonth, endMonth, searchType));
    }

    /**
     * 1: >24小时停机次数按月统计
     * 统计规则：
     * 1：非计划性维修
     * 2：报修和维修结束的时间大于24小时,即设备停机修复时间
     * <p>
     * 2：设备故障次数按月统计
     * 统计规则：
     * 1: 非计划性维修
     * 2: 时间不限制
     *
     * @param dto  查询条件
     * @param year 查询年份
     * @return R
     */
    @GetMapping("/export")
    @SysLog("24小时停机次数按月统计导出")
    public void export(HttpServletResponse response, RepairApplicationFormDTO dto, @RequestParam(required = false) Integer year, @RequestParam(required = false) LocalDate startMonth, @RequestParam(required = false) LocalDate endMonth, @RequestParam(defaultValue = "1") Integer searchType) {
        List<RepairReport24VO> list = repairApplicationFormService.getMonthReportBig24(dto, year, startMonth, endMonth, searchType);
        String fileName = "";
        if (year == null) {
            fileName = DateUtils.dateToString(startMonth, DateUtils.PATTERN_YMD) + "-" + DateUtils.dateToString(endMonth, DateUtils.PATTERN_YMD) + (searchType == 1 ? "-24小时停机次数按月统计表" : "年-设备故障次数按月统计");
        } else {
            fileName = year + (searchType == 1 ? "年-24小时停机次数按月统计表" : "年-设备故障次数按月统计");
        }
        ExcelUtil.exportResponseDict(response, RepairReport24VO.class, list, fileName);
    }

    /**
     * 1: >24小时停机次数按月统计
     * 统计规则：
     * 1：非计划性维修
     * 2：报修和维修结束的时间大于24小时,即设备停机修复时间
     * <p>
     * 2：设备故障次数按月统计
     * 统计规则：
     * 1: 非计划性维修
     * 2: 时间不限制
     *
     * @param dto  查询条件
     * @param year 查询年份
     * @return R
     */
    @GetMapping("/export/month")
    @SysLog("24小时停机次数每月明细导出")
    public void export(HttpServletResponse response, RepairApplicationFormDTO dto, @RequestParam(required = false) Integer year, @RequestParam(required = false) LocalDate startMonth, @RequestParam(required = false) LocalDate endMonth, @RequestParam(defaultValue = "1") Integer searchType, @RequestParam(required = false) Integer month) {
        List<RepairReport24VO> list = repairApplicationFormService.getMonthReportBig24(dto, year, startMonth, endMonth, searchType);
        String fileName = "";
        fileName = year + "-" + month + (searchType == 1 ? "-24小时停机次数按月统计表" : "-设备故障次数按月统计");
        if (CollectionUtil.isNotEmpty(list)) {
            for (RepairReport24VO vo : list) {
                if (vo.getMonth() == month) {
                    List<RepairApplicationFormVO> detailList = vo.getDetailList();
                    if (CollectionUtil.isNotEmpty(detailList)) {
                        ExcelUtil.exportResponseDict(response, ExportRepairApplicationFormVO.class, BeanConverterUtil.copyListProperties(detailList, ExportRepairApplicationFormVO.class), fileName);
                    }
                }
            }
        }
    }

    /**
     * MTTR月统计报表
     * 统计规则：
     * 1：所有非计划性维修，且dealMinutes小于24小时的
     * 2：所有维修时间累加除以每月的次数
     *
     * @param dto  查询条件
     * @param year 查询年份
     * @return R
     */
    @GetMapping("mttr")
    @SysLog("MTTR月统计报表")
    public R getMonthReportMttr(RepairApplicationFormDTO dto, @RequestParam(required = false) Integer year, @RequestParam(required = false) LocalDate startMonth, @RequestParam(required = false) LocalDate endMonth, @RequestParam(defaultValue = "1") Integer searchType) {
        return new R(repairApplicationFormService.getMonthReportMtrr(dto, year, startMonth, endMonth));
    }

    /**
     * 1: >24小时停机次数按月统计
     * 2: 设备故障次数按月统计
     * 统计规则：
     * 1：非计划性维修，且dealMinutes小于24小时的
     * 2：报修和维修结束的时间大于24小时,即设备停机修复时间
     *
     * @param dto  查询条件
     * @param year 查询年份
     * @return R
     */
    @GetMapping("/mttr/export")
    @SysLog("MTTR月统计报表导出")
    public void exportMttr(HttpServletResponse response, RepairApplicationFormDTO dto, @RequestParam(required = false) Integer year, @RequestParam(required = false) LocalDate startMonth, @RequestParam(required = false) LocalDate endMonth) {
        List<RepairReportMttr> list = repairApplicationFormService.getMonthReportMtrr(dto, year, startMonth, endMonth);
        String fileName = "";
        if (year == null) {
            fileName = DateUtils.dateToString(startMonth, DateUtils.PATTERN_YMD) + "-" + DateUtils.dateToString(endMonth, DateUtils.PATTERN_YMD) + "-MTTR月统计报表";
        } else {
            fileName = year + "年-MTTR月统计报表";
        }
        ExcelUtil.exportResponseDict(response, RepairReportMttr.class, list, fileName);
    }

    /**
     * 1: >24小时停机次数按月统计
     * 2: 设备故障次数按月统计
     * 统计规则：
     * 1：非计划性维修，且dealMinutes小于24小时的
     * 2：报修和维修结束的时间大于24小时,即设备停机修复时间
     *
     * @param dto  查询条件
     * @param year 查询年份
     * @return R
     */
    @GetMapping("/mttr/export/month")
    @SysLog("MTTR月统计明细导出")
    public void exportMttr(HttpServletResponse response, RepairApplicationFormDTO dto,
                           @RequestParam(required = false) Integer year, @RequestParam(required = false) LocalDate startMonth, @RequestParam(required = false) LocalDate endMonth,
                           @RequestParam(required = false) Integer month) {
        List<RepairReportMttr> list = repairApplicationFormService.getMonthReportMtrr(dto, year, startMonth, endMonth);
        String fileName = year + "-" + month + "-MTTR月统计明细";
        if (CollectionUtil.isNotEmpty(list)) {
            for (RepairReportMttr vo : list) {
                if (vo.getMonth() == month) {
                    List<RepairApplicationFormVO> detailList = vo.getDetailList();
                    if (CollectionUtil.isNotEmpty(detailList)) {
                        ExcelUtil.exportResponseDict(response, ExportRepairApplicationFormVO.class, BeanConverterUtil.copyListProperties(detailList, ExportRepairApplicationFormVO.class), fileName);
                    }
                }
            }
        }
    }

    /**
     * 费用月统计报表
     * 统计规则：
     * 1：所有费用
     *
     * @param dto  查询条件
     * @param year 查询年份
     * @return R
     */
    @GetMapping("fee")
    @SysLog("费用月统计报表")
    public R getMonthReportFee(RepairFeeDTO dto, @RequestParam(required = false) Integer year, @RequestParam(required = false) LocalDate startMonth, @RequestParam(required = false) LocalDate endMonth, @RequestParam(defaultValue = "1") Integer searchType) {
        return new R(repairFeeService.getMonthReport(dto, year, startMonth, endMonth));
    }

    /**
     * 费用月统计报表
     * 统计规则：
     * 1：所有费用
     *
     * @param dto  查询条件
     * @param year 查询年份
     * @return R
     */
    @GetMapping("/fee/export")
    @SysLog("费用月统计报表导出")
    public void exportFee(HttpServletResponse response, RepairFeeDTO dto, @RequestParam(required = false) Integer year, @RequestParam(required = false) LocalDate startMonth, @RequestParam(required = false) LocalDate endMonth) {
        List<RepairReportFee> list = repairFeeService.getMonthReport(dto, year, startMonth, endMonth);
        String fileName = "";
        if (year == null) {
            fileName = DateUtils.dateToString(startMonth, DateUtils.PATTERN_YMD) + "-" + DateUtils.dateToString(endMonth, DateUtils.PATTERN_YMD) + "-费用月统计报表";
        } else {
            fileName = year + "年-费用月统计报表";
        }
        ExcelUtil.exportResponseDict(response, RepairReportFee.class, list, fileName);
    }

    /**
     * 费用月统计报表
     * 统计规则：
     * 1：所有费用
     *
     * @param dto  查询条件
     * @param year 查询年份
     * @return R
     */
    @GetMapping("/fee/export/month")
    @SysLog("费用月明细导出")
    public void exportFee(HttpServletResponse response, RepairFeeDTO dto, @RequestParam(required = false) Integer year,
                          @RequestParam(required = false) LocalDate startMonth, @RequestParam(required = false) LocalDate endMonth,
                          @RequestParam(required = false) Integer month) {
        List<RepairReportFee> list = repairFeeService.getMonthReport(dto, year, startMonth, endMonth);
        String fileName = year + "-" + month + "-费用月明细";
        if (CollectionUtil.isNotEmpty(list)) {
            for (RepairReportFee vo : list) {
                if (vo.getMonth() == month) {
                    List<RepairFee> detailList = vo.getDetailList();
                    if (CollectionUtil.isNotEmpty(detailList)) {
                        ExcelUtil.exportResponseDict(response, ExportRepairFeeVO.class, BeanConverterUtil.copyListProperties(detailList, ExportRepairFeeVO.class), fileName);
                    }else{
                        ExcelUtil.exportResponseDict(response, ExportRepairFeeVO.class, BeanConverterUtil.copyListProperties(detailList, ExportRepairFeeVO.class), fileName);
                    }
                }
            }
        }
    }

    /**
     * 1：某个设备一年的故障统计仪表盘，多少次维修。非计划性维修多少次，计划性维修多少次，柱状图，按照年统计：2021，2022.
     * 统计规则：
     * 1：堆垛累加
     * 2：总次数，计划性和非计划性各多少个
     * 3：寿命曲线图，从哪一年开始的，到结束。如寿命5年，从2019年开始，则2019 5，2020 4，2021 3，2022 2，2023 1， 2024 0
     *
     * @param dto 查询条件
     * @return R
     */
    @GetMapping("sbInfo")
    @SysLog("设备维修统计")
    public R getSbInfoReport(RepairApplicationFormDTO dto, @RequestParam(required = false) Integer year) {
        List<RepairSbInfoReport> repairSbInfoReports = repairApplicationFormService.getSbInfoReport(dto, year);
        JSONObject result = new JSONObject();
        // 转化列表为叠装柱状图需要的数据形式
        JSONArray jsonArray = new JSONArray();
        if (CollectionUtil.isNotEmpty(repairSbInfoReports)) {
            JSONObject plan = new JSONObject();
            plan.put("name", "计划性");
            JSONObject planNot = new JSONObject();
            planNot.put("name", "非计划性");
            List<Integer> yearList = new ArrayList<>();
            for (RepairSbInfoReport repairSbInfoReport : repairSbInfoReports) {
                plan.put(repairSbInfoReport.getYear() + "", repairSbInfoReport.getNum());
                planNot.put(repairSbInfoReport.getYear() + "", repairSbInfoReport.getNum());
                yearList.add(repairSbInfoReport.getYear());
            }
            jsonArray.add(plan);
            jsonArray.add(planNot);
            result.put("data", jsonArray);
            result.put("fields", yearList);
            result.put("dataTable", repairSbInfoReports);
        }
        // log.info(result.toJSONString());
        return new R(result);
    }

    @GetMapping("/sbInfo/export")
    @SysLog("设备维修统计导出")
    public void exportSbInfo(HttpServletResponse response, RepairApplicationFormDTO dto, @RequestParam(required = false) Integer year) {
        List<RepairSbInfoReport> list = repairApplicationFormService.getSbInfoReport(dto, year);
        ExcelUtil.exportResponseDict(response, RepairSbInfoReport.class, list, "设备维修统计导出");
    }

    @GetMapping("/sbInfo/export/year")
    @SysLog("设备维修统计明细导出")
    public void exportSbInfoYear(HttpServletResponse response, RepairApplicationFormDTO dto, @RequestParam(required = false) Integer year) {
        List<RepairSbInfoReport> list = repairApplicationFormService.getSbInfoReport(dto, year);
        String fileName = "";
        fileName = year + "-设备维修统计明细导出";
        if (CollectionUtil.isNotEmpty(list)) {
            for (RepairSbInfoReport vo : list) {
                if (vo.getYear().equals(year)) {
                    List<RepairApplicationFormVO> detailList = vo.getDetailList();
                    if (CollectionUtil.isNotEmpty(detailList)) {
                        ExcelUtil.exportResponseDict(response, ExportRepairApplicationFormVO.class, BeanConverterUtil.copyListProperties(detailList, ExportRepairApplicationFormVO.class), fileName);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Integer year = 2021;
        Integer ss = 2021;
        System.out.println(year == ss);
    }

    /**
     * 费用月统计报表
     * 统计规则：
     * 1：所有费用
     *
     * @param dto  查询条件
     * @param year 查询年份
     * @return R
     */
    @GetMapping("/sbInfo/fee")
    @SysLog("设备费用统计报表")
    public R getSbInfoReportFee(RepairFeeDTO dto, @RequestParam(required = false) Integer year) {
        List<RepairSbInfoReportFee> repairSbInfoReports = repairFeeService.getSbInfoReport(dto, year);
        JSONObject result = new JSONObject();
        // 转化列表为叠装柱状图需要的数据形式
        JSONArray jsonArray = new JSONArray();
        if (CollectionUtil.isNotEmpty(repairSbInfoReports)) {
            JSONObject plan = new JSONObject();
            plan.put("name", "计划性");
            JSONObject planNot = new JSONObject();
            planNot.put("name", "非计划性");
            List<Integer> yearList = new ArrayList<>();
            for (RepairSbInfoReportFee repairSbInfoReport : repairSbInfoReports) {
                plan.put(repairSbInfoReport.getYear() + "", repairSbInfoReport.getNum());
                planNot.put(repairSbInfoReport.getYear() + "", repairSbInfoReport.getNum());
                yearList.add(repairSbInfoReport.getYear());
            }
            jsonArray.add(plan);
            jsonArray.add(planNot);
            result.put("data", jsonArray);
            result.put("fields", yearList);
            result.put("dataTable", repairSbInfoReports);
        }
        //log.info(result.toJSONString());
        return new R(result);
    }

    /**
     * 费用月统计报表
     * 统计规则：
     * 1：所有费用
     *
     * @param dto  查询条件
     * @param year 查询年份
     * @return R
     */
    @GetMapping("/sbInfo/fee/export")
    @SysLog("设备费用统计报表导出")
    public void exportSbInfoFee(HttpServletResponse response, RepairFeeDTO dto, @RequestParam(required = false) Integer year) {
        List<RepairSbInfoReportFee> list = repairFeeService.getSbInfoReport(dto, year);
        String fileName = "设备费用统计报表";
        ExcelUtil.exportResponseDict(response, RepairSbInfoReportFee.class, list, fileName);
    }

    /**
     * 费用统计报表
     * 统计规则：
     * 1：所有费用
     *
     * @param dto  查询条件
     * @param year 查询年份
     * @return R
     */
    @GetMapping("/sbInfo/fee/export/year")
    @SysLog("费用月明细导出")
    public void exportSbInfoFeeYear(HttpServletResponse response, RepairFeeDTO dto, @RequestParam(required = false) Integer year) {
        List<RepairSbInfoReportFee> list = repairFeeService.getSbInfoReport(dto, year);
        String fileName = year + "-费用月明细";
        if (CollectionUtil.isNotEmpty(list)) {
            for (RepairSbInfoReportFee vo : list) {
                if (vo.getYear().equals(year)) {
                    List<RepairFee> detailList = vo.getDetailList();
                    if (CollectionUtil.isNotEmpty(detailList)) {
                        ExcelUtil.exportResponseDict(response, ExportRepairFeeVO.class, BeanConverterUtil.copyListProperties(detailList, ExportRepairFeeVO.class), fileName);
                    }
                }
            }
        }
    }
}
